package com.demo.petshop.controller;

import com.demo.petshop.entity.Favorites;
import com.demo.petshop.service.FavoritesService;
import com.demo.petshop.util.Result;
import com.mybatisflex.core.paginate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author lenovo
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/favorites")
public class FavoritesController {

    @Autowired
    private FavoritesService favoritesService;

    @PostMapping("save")
    public boolean save(@RequestBody Favorites favorites) {
        return favoritesService.save(favorites);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return favoritesService.removeById(id);
    }

    @PutMapping("update")
    public boolean update(@RequestBody Favorites favorites) {
        return favoritesService.updateById(favorites);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<Favorites> list() {
        return favoritesService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public Favorites getInfo(@PathVariable Serializable id) {
        return favoritesService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Favorites> page(Page<Favorites> page) {
        return favoritesService.page(page);
    }

    @GetMapping("addFavorite")
    public Object addFavorite(@RequestParam String type,@RequestParam int id){
        return favoritesService.addFavorite(type,id);
    }


    @GetMapping("removeFavorite")
    public Object removeFavorite(@RequestParam String type,@RequestParam int id){
        return favoritesService.removeFavorite(type,id);
    }


    @GetMapping("getFavorite")
    public Object getFavorite(@RequestParam String type, @RequestParam(required = false) Integer userId){
        return favoritesService.getFavorite(type, userId);
    }


    @GetMapping("checkFavorite")
    public Object checkFavorite(@RequestParam String type,@RequestParam int id){
        return favoritesService.checkFavorite(type,id);
    }

    /**
     * 获取用户收藏数量
     * @param userId 用户ID，可选参数
     * @return 收藏数量
     */
    @GetMapping("getCount")
    public Result getCount(@RequestParam(required = false) Integer userId) {
        // 如果userId为空，使用当前登录用户
        return Result.success(favoritesService.getCount(userId));
    }

    @GetMapping("getFavoriteNumber")
    public Object getFavoriteNumber(@RequestParam String type,@RequestParam int id){
        return favoritesService.getFavoriteNumber(type,id);
    }
}
